<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::NamedScope::ClassMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::NamedScope::ClassMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/activerecord/lib/active_record/named_scope_rb.html">
                vendor/rails/activerecord/lib/active_record/named_scope.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001610">named_scope</a>&nbsp;&nbsp;
      <a href="#M001609">scopes</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001610" class="method-detail">
        <a name="M001610"></a>

        <div class="method-heading">
          <a href="#M001610" class="method-signature">
          <span class="method-name">named_scope</span><span class="method-args">(name, options = {}, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Adds a class method for retrieving and querying objects. A scope represents
a narrowing of a database query, such as <tt>:conditions =&gt; {:color
=&gt; :red}, :select =&gt; &#8216;shirts.*&#8217;, :include =&gt;
:washing_instructions</tt>.
</p>
<pre>
  class Shirt &lt; ActiveRecord::Base
    named_scope :red, :conditions =&gt; {:color =&gt; 'red'}
    named_scope :dry_clean_only, :joins =&gt; :washing_instructions, :conditions =&gt; ['washing_instructions.dry_clean_only = ?', true]
  end
</pre>
<p>
The above calls to <tt><a
href="ClassMethods.html#M001610">named_scope</a></tt> define class methods
Shirt.red and Shirt.dry_clean_only. Shirt.red, in effect, represents the
query <tt>Shirt.find(:all, :conditions =&gt; {:color =&gt;
&#8216;red&#8217;})</tt>.
</p>
<p>
Unlike <tt>Shirt.find(...)</tt>, however, the object returned by Shirt.red
is not an Array; it resembles the association object constructed by a
<tt>has_many</tt> declaration. For instance, you can invoke
<tt>Shirt.red.find(:first)</tt>, <tt>Shirt.red.count</tt>,
<tt>Shirt.red.find(:all, :conditions =&gt; {:size =&gt;
&#8216;small&#8217;})</tt>. Also, just as with the association objects,
named \<a href="ClassMethods.html#M001609">scopes</a> act like an Array,
implementing <a href="../../Enumerable.html">Enumerable</a>;
<tt>Shirt.red.each(&amp;block)</tt>, <tt>Shirt.red.first</tt>, and
<tt>Shirt.red.inject(memo, &amp;block)</tt> all behave as if Shirt.red
really was an Array.
</p>
<p>
These named \<a href="ClassMethods.html#M001609">scopes</a> are composable.
For instance, <tt>Shirt.red.dry_clean_only</tt> will produce all shirts
that are both red and dry clean only. Nested finds and calculations also
work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt>
returns the number of garments for which these criteria obtain. Similarly
with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
</p>
<p>
All \<a href="ClassMethods.html#M001609">scopes</a> are available as class
methods on the <a href="../Base.html">ActiveRecord::Base</a> descendent
upon which the \<a href="ClassMethods.html#M001609">scopes</a> were
defined. But they are also available to <tt>has_many</tt> associations. If,
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    has_many :shirts
  end
</pre>
<p>
then <tt>elton.shirts.red.dry_clean_only</tt> will return all of
Elton&#8216;s red, dry clean only shirts.
</p>
<p>
Named \<a href="ClassMethods.html#M001609">scopes</a> can also be
procedural:
</p>
<pre>
  class Shirt &lt; ActiveRecord::Base
    named_scope :colored, lambda { |color|
      { :conditions =&gt; { :color =&gt; color } }
    }
  end
</pre>
<p>
In this example, <tt>Shirt.colored(&#8216;puce&#8217;)</tt> finds all puce
shirts.
</p>
<p>
Named \<a href="ClassMethods.html#M001609">scopes</a> can also have
extensions, just as with <tt>has_many</tt> declarations:
</p>
<pre>
  class Shirt &lt; ActiveRecord::Base
    named_scope :red, :conditions =&gt; {:color =&gt; 'red'} do
      def dom_id
        'red_shirts'
      end
    end
  end
</pre>
<p>
For testing complex named \<a href="ClassMethods.html#M001609">scopes</a>,
you can examine the scoping options using the <tt>proxy_options</tt> method
on the proxy itself.
</p>
<pre>
  class Shirt &lt; ActiveRecord::Base
    named_scope :colored, lambda { |color|
      { :conditions =&gt; { :color =&gt; color } }
    }
  end

  expected_options = { :conditions =&gt; { :colored =&gt; 'red' } }
  assert_equal expected_options, Shirt.colored('red').proxy_options
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001610-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001610-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/named_scope.rb, line 84</span>
84:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">named_scope</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
85:         <span class="ruby-identifier">name</span> = <span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_sym</span>
86:         <span class="ruby-identifier">scopes</span>[<span class="ruby-identifier">name</span>] = <span class="ruby-identifier">lambda</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">parent_scope</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span><span class="ruby-operator">|</span>
87:           <span class="ruby-constant">Scope</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">parent_scope</span>, <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">options</span>
88:             <span class="ruby-keyword kw">when</span> <span class="ruby-constant">Hash</span>
89:               <span class="ruby-identifier">options</span>
90:             <span class="ruby-keyword kw">when</span> <span class="ruby-constant">Proc</span>
91:               <span class="ruby-identifier">options</span>.<span class="ruby-identifier">call</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
92:           <span class="ruby-keyword kw">end</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
93:         <span class="ruby-keyword kw">end</span>
94:         (<span class="ruby-keyword kw">class</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword kw">self</span>; <span class="ruby-keyword kw">self</span> <span class="ruby-keyword kw">end</span>).<span class="ruby-identifier">instance_eval</span> <span class="ruby-keyword kw">do</span>
95:           <span class="ruby-identifier">define_method</span> <span class="ruby-identifier">name</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-operator">*</span><span class="ruby-identifier">args</span><span class="ruby-operator">|</span>
96:             <span class="ruby-identifier">scopes</span>[<span class="ruby-identifier">name</span>].<span class="ruby-identifier">call</span>(<span class="ruby-keyword kw">self</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
97:           <span class="ruby-keyword kw">end</span>
98:         <span class="ruby-keyword kw">end</span>
99:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001609" class="method-detail">
        <a name="M001609"></a>

        <div class="method-heading">
          <a href="#M001609" class="method-signature">
          <span class="method-name">scopes</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001609-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001609-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/named_scope.rb, line 17</span>
17:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">scopes</span>
18:         <span class="ruby-identifier">read_inheritable_attribute</span>(<span class="ruby-identifier">:scopes</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">write_inheritable_attribute</span>(<span class="ruby-identifier">:scopes</span>, {})
19:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>